package mapEditor.interactor;

//import debug.*;
	
import mapEditor.map.MapCanvas;
import mapEditor.obj.PathObject;
import mapEditor.utils.MapLocation;

import java.awt.Graphics2D;


// ********************************************************************
// *** CLASS: RectangleMouseAdapter
// ********************************************************************
public class DrawParallelogramInteractor extends MapInteractorAdapter
{ public String getMouseModeMnemonic() { return("Draw Parallelogram"); }
  private PathObject polygon; // !null when drawing
  private MapLocation p0;
  private MapLocation p1;
  private MapLocation p2;
  private MapLocation p3;


  // ******************************************************************
  // *** CONSTRUCTOR: RectangleMouseAdapter
  // ******************************************************************
  public DrawParallelogramInteractor(MapCanvas mapCanvas)
  { super(mapCanvas,DRAW_PARALLELOGRAM); }

  // ******************************************************************
  // *** METHOD: paintMouseEffect
  // ******************************************************************
  public void paintMouseEffect(Graphics2D g)
  {	if(polygon!=null) polygon.paint((Graphics2D)g); }

  // ******************************************************************
  // *** METHOD: mouseDragged
  // ******************************************************************
  // Invoked when a mouse button is pressed and then dragged.
  protected void mouseDragged()
  { if(polygon!=null)
    { if(p2==null)
      { p1.setX(mouseCurrent.getX());
        p1.setY(mouseCurrent.getY());
      }
      else
      { p2.setX(mouseCurrent.getX());
        p2.setY(mouseCurrent.getY());
        double dx=p1.getX()-p0.getX();
        double dy=p1.getY()-p0.getY();
   	    p3.setX(p2.getX()-dx);
   	    p3.setY(p2.getY()-dy);
      }
	  mapCanvas.repaint();
	}
  }


  // ******************************************************************
  // *** METHOD: mouseMoved
  // ******************************************************************
  // Invoked when the mouse has been moved (with no buttons down).
  protected void mouseMoved() {}

  // ******************************************************************
  // *** METHOD: mousePressed
  // ******************************************************************
  // Invoked when a mouse button has been pressed.
  protected void mouseLeftPressed(MapLocation p)
  { if(polygon==null)
	{ p0=p; p1=p.copy(); p2=null; p3=null;
	  polygon=new PathObject(mapCanvas.getDOMTree(),p0);
	  polygon.add(p1);
	}
	else
	{ p2=p; p3=p.copy();
	  polygon.add(p2);
	  polygon.add(p3);
	  polygon.setClosedPath(true);
    }
	mapCanvas.repaint();
  }
  protected void mouseRightPressed(MapLocation p) {}

  // ******************************************************************
  // *** METHOD: mouseReleased
  // ******************************************************************
  // Invoked when a mouse button has been released.
  protected void mouseLeftReleased(MapLocation p)
  { if(polygon!=null)
    { if(p2!=null)
      { mapCanvas.drawObjectDone(polygon,"PARALLELOGRAM");  
        polygon=null;
      } else mouseStart=p;
      mapCanvas.repaint();
    }
  }
  protected void mouseRightReleased(MapLocation p) {}

}